/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/7/5
 * Description:版权所有 QQ:376231445
 */
package com.it1997.it1997admin.utils;
import com.it1997.it1997admin.pojo.KsData;
import com.it1997.it1997admin.pojo.OtherKmKsNumber;
import com.it1997.it1997admin.service.ExtractDataService;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.log4j.Log4j2;

import java.io.FileOutputStream;
import java.util.List;

@Log4j2
public class XkCreatePdf implements Runnable {
    private ExtractDataService extractDataService;
    private List<KsData> ksDataList;
    private String NewPath;
    private String KDDM;
    private Integer KCSL;
    private Integer KSSL;

    public XkCreatePdf(ExtractDataService extractDataService, List<KsData> ksDataList, String newPath, String KDDM, Integer KCSL, Integer KSSL) {
        this.extractDataService = extractDataService;
        this.ksDataList = ksDataList;
        NewPath = newPath;
        this.KDDM = KDDM;
        this.KCSL = KCSL;
        this.KSSL = KSSL;
    }
    @Override
    public void run() {
        createPdf();
    }
    private synchronized void createPdf(){
                String FONT = "/static/font.ttf";//pdf字体
                Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 20);
                try {
                    Document document = new Document();
                    PdfWriter.getInstance(document, new FileOutputStream(NewPath));
                    document.open();
            for (KsData KS : ksDataList) {
                String imgPath = "D:/data/Photo2016/" + KS.getKSH().substring(4,8)+"/"+KS.getKSH().substring(4,10)+"/"+KS.getKSH().substring(4,14)+"/"+KS.getKSH().substring(14,15)+"/"+KS.getKSH().substring(0,4) + "/" + KS.getKSH() + ".jpg";
                Image image = Image.getInstance(imgPath);
                image.setAlignment(Image.MIDDLE);

                document.add(new Paragraph("考点:【" + KDDM + "】" + "总考场数：" + KCSL, f1));
                document.add(new Paragraph("【姓名】：" + KS.getXM(), f1));
                document.add(new Paragraph("【考生号】：" + KS.getKSH(), f1));
                document.add(new Paragraph("【证件号】：" + KS.getZJHM(), f1));
                document.add(new Paragraph("================以下是科目信息================", f1));
                log.info("该考生的考生号：" + KS.getKSH());
                List<KsData> ksDataList1 = extractDataService.getRandomFkKs(KS.getKSH());
                log.info("该考生的科目信息：" + ksDataList1);
                for (KsData KS2 : ksDataList1) {
                    document.add(new Paragraph("【科目】：" + KS2.getKMDM() + "【考场】：" + KS2.getKCDM() + "【座号】：" + KS2.getZH(), f1));
                }
                List<OtherKmKsNumber> otherKmKsNumberList=extractDataService.getOtherKmKsNumber(KDDM);
                document.add(new Paragraph("==============该考点各科目总人数==============", f1));
                //document.add(new Paragraph("【科目】：" + "语数外" + "【人数】：" + KSSL, f1));
                for (OtherKmKsNumber KS3 : otherKmKsNumberList) {
                    document.add(new Paragraph("【科目】：" + KS3.getKMMC()+ "【人数】：" + KS3.getKSSL(), f1));
                }
                document.add(image);
            }
            document.close();
        }catch (Exception e){
            log.error(e.getMessage());
        }

    }

}
